#include "append.h"
#include "config.h"

Append::Append(TableMapping &tableMapping, Oracle::Connection &connection):Load(tableMapping, connection)
{
}

int Append::load()
{
	return Load::load();
}

int Append::createSql()
{
	bool first = true;
	const Table &tagetTable = tableMapping.getTargetTable();
	map<string, string> &mapping = tableMapping.getMap();
	map<string, string>::const_iterator iter;
	char tmp[1024];
	string insertCols, insertValues;
	
	sql  = "insert into ";
	sql += tableMapping.getTargetTable().getName();
	
	insertCols = " a (";
	insertValues = "(select ";	
	for (iter=mapping.begin(),first=true; iter!=mapping.end(); iter++){
		
		if (!first){
			insertCols += ",";
			insertValues += ",";
		}
		first = false;
				
		sprintf(tmp, "%s", iter->first.c_str());
		insertCols += tmp;
		
		sprintf(tmp, "%s", iter->second.c_str());
		insertValues += tmp;
	}
	insertCols += ") ";
	insertValues += " from ";
	insertValues += tableMapping.getSourceTable().getName();
	insertValues += " b)";
	
	sql += insertCols;
	sql += insertValues;	
	
	return 0;	
}
